package dao

import (
	"TodoList/logm"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"log"
)

// PgDB 数据库链接单例
var PgDB *gorm.DB

func DataBase() {
	db, err := gorm.Open(postgres.New(postgres.Config{
		DSN:                  "host=localhost user=root password=postgres dbname=todoList port=5432 sslmode=disable TimeZone=Asia/Shanghai",
		PreferSimpleProtocol: true, // disables implicit prepared statement usage
	}), &gorm.Config{})
	if err != nil {
		log.Fatal("failed to connect pg", err)
	}
	sqlDB, err := db.DB()
	if err != nil {
		logm.Sad.Fatal("connect postgres failed:", err)
	}
	//设置连接池
	//空闲
	sqlDB.SetMaxIdleConns(10)
	//打开
	sqlDB.SetMaxOpenConns(20)
	//数据迁移，进行建表改表操作
	PgDB = db
	Migration()
}
